#ifndef __PG_DB_CONNECTION_H_
#define __PG_DB_CONNECTION_H_

#include <libpq-fe.h>
#include <map>
#include "Debug.h"
#include "Regex_KV_Mapping.h"

using namespace std;

class Farm;
class PqPool
{
	private:
		static PqPool *_instance;
                bool m_bDebug;
	public:
		static PqPool *Instance(const char *confFile = NULL);
		static void Release();
		~PqPool(); 
		int GetWriteCounts(int farmID);
		PGconn* GetReader(int farmID);
		PGconn* GetWriter(int farmID, int indx);
		void CloseReader(int farmID, PGconn *c);
		void CloseWriter(int farmID, PGconn *c);
		int GetFarmsCounts() { return (int)pcMap.size();  }
                void Print();
                int GetIDbyKey(int key);
                int GetMaxKey() { return m_iMax; } 
	protected:
		PqPool() { m_bDebug = false;  m_MappingIdex = NULL;  }
		bool init(const char *confFile); 
		void closeAll();
	private:
                int m_iMax;
		map<int, Farm* > pcMap;
                MappingIndex *m_MappingIdex;

};


#endif
